package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.ParsingException;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.ARRAY;
import ar.edu.caece.compiler.grammar.nonterminals.TEXTO2;
import ar.edu.caece.compiler.grammar.nonterminals.TEXTOA;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;
import ar.edu.caece.compiler.grammar.terminals.IdentifierToken;

public class TEXTO2Factory extends AbstractTokenVisitorFactory {

	private static TEXTO2Factory INSTANCE;
	
	private TEXTOAFactory textoaFactory;
	private TokenVisitorInterface parseTEXTOA;
	private ARRAYFactory arrayFactory;
	private TokenVisitorInterface parseARRAY;
	
	public static TEXTO2Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new TEXTO2Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			textoaFactory = TEXTOAFactory.getInstance();
			parseTEXTOA = textoaFactory.makeVisitor();
			
			arrayFactory = ARRAYFactory.getInstance();
			parseARRAY = arrayFactory.makeVisitor();
		}
	};
	
	private TEXTO2Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierChainVisitor(siguiente) {
			
			@Override
			public GrammarSymbol identifierCase(IdentifierToken llamador) throws Exception {
				return new TEXTO2(llamador, (ARRAY) nextToken().execute(parseARRAY),(TEXTOA) nextToken().execute(parseTEXTOA));
			}
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierTokenVisitor() {
			
			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				throw new ParsingException("Error de parsing.",TEXTO2.class,llamador);
			}
			
			@Override
			public GrammarSymbol identifierCase(IdentifierToken llamador) throws Exception {
				return new TEXTO2(llamador, (ARRAY) nextToken().execute(parseARRAY), (TEXTOA) nextToken().execute(parseTEXTOA));
			}
		};
	}
	
	
}
